import 'package:flutter/material.dart';

//  尺寸限制类容器
class SizeLimitRoute extends StatefulWidget {
  @override
  _SizeLimitRouteState createState() => _SizeLimitRouteState();
}

class _SizeLimitRouteState extends State<SizeLimitRoute> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("尺寸限制类容器"),
        actions: <Widget>[
          UnconstrainedBox(
            child: SizedBox(
              width: 20.0,
              height: 20.0,
              child: CircularProgressIndicator(
                strokeWidth: 3,
                valueColor: AlwaysStoppedAnimation(Colors.white70),
              ),
            ),
          )
        ],
      ),
      body: _SizeLimitRouteBody(),
    );
  }
}

class _SizeLimitRouteBody extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var redBox = DecoratedBox(
      decoration: BoxDecoration(color: Colors.red),
    );
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Text("ConstrainedBox"),
        ConstrainedBox(
          constraints: BoxConstraints(
              minWidth: double.infinity, //宽度尽可能大
              minHeight: 60.0 // 最小高度为50像素
              ),
          child: Container(
            height: 80.0,
            child: redBox,
          ),
        ),
        Text("SizeBox"),
        SizedBox(
          width: 80.0,
          height: 80.0,
          child: redBox,
        ),
        Text("SizedBox只是ConstrainedBox"),
        ConstrainedBox(
          constraints: BoxConstraints.tightFor(width: 80.0, height: 80.0),
          child: redBox,
        ),
        Text("多重限制1"),
        ConstrainedBox(
            constraints: BoxConstraints(minWidth: 60.0, minHeight: 60.0), //父
            child: ConstrainedBox(
              constraints: BoxConstraints(minWidth: 90.0, minHeight: 20.0), //子
              child: redBox,
            )),
        Text("多重限制2"),
        ConstrainedBox(
            constraints: BoxConstraints(minWidth: 90.0, minHeight: 20.0),
            child: ConstrainedBox(
              constraints: BoxConstraints(minWidth: 60.0, minHeight: 60.0),
              child: redBox,
            )),
        Text("UnconstrainedBox"),
        ConstrainedBox(
          constraints: BoxConstraints(minWidth: 60.0, minHeight: 100.0), //父子
          child: UnconstrainedBox(
            //去除"父级限制"
            child: ConstrainedBox(
              constraints: BoxConstraints(minWidth: 90.0, minHeight: 20.0), //子
              child: redBox,
            ),
          ),
        ),
      ],
    );
  }
}
